package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.yangzihui.*;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.service.TopicService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author yangzihui
 * @Description
 * @Create 2022-06-26 18:36
 * @Version
 */
@Service
@Transactional
public class TopicServiceImpl implements TopicService {
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public TopicListVO query(BaseParam baseParam, String subtitle, String title) {
        subtitle = StringUtils.isBlank(subtitle) ? null : ("%" + subtitle + "%");
        title = StringUtils.isBlank(title) ? null : ("%" + title + "%");
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        if (!StringUtils.isBlank(subtitle)) {
            criteria.andSubtitleLike(subtitle);
        }
        if (!StringUtils.isBlank(title)) {
            criteria.andTitleLike(title);
        }
        criteria.andDeletedEqualTo(false);
        marketTopicExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);
        PageInfo pageInfo = new PageInfo(marketTopics);
        TopicListVO topicListVO = new TopicListVO();
        topicListVO.setList(marketTopics);
        topicListVO.setLimit(baseParam.getLimit());
        topicListVO.setTotal(pageInfo.getTotal());
        topicListVO.setPages(pageInfo.getPages());
        topicListVO.setPage(baseParam.getPage());
        return topicListVO;
    }

    @Override
    public DataDTOXX readTopic(Integer id) {
        MarketTopic topic = marketTopicMapper.selectByPrimaryKey(id);
        Integer[] goodsIds = new Integer[0];
        if (topic.getGoods() != null) {
            try {
                goodsIds = objectMapper.readValue(topic.getGoods(), Integer[].class);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        List<Integer> goodsIdList = Arrays.asList(goodsIds);
        TopicDTO topicDTO = new TopicDTO();
        topicDTO.setId(topic.getId());
        topicDTO.setTitle(topic.getTitle());
        topicDTO.setSubtitle(topic.getSubtitle());
        topicDTO.setPrice(topic.getPrice());
        topicDTO.setReadCount(topic.getReadCount());
        topicDTO.setPicUrl(topic.getPicUrl());
        topicDTO.setSortOrder(topic.getSortOrder());
        topicDTO.setAddTime(topic.getUpdateTime());
        topicDTO.setDeleted(topic.getDeleted());
        topicDTO.setContent(topic.getContent());
        topicDTO.setGoods(goodsIdList);

        List<GoodsListDTO> goodsListDTOList = new ArrayList<>();
        for (Integer integer : goodsIdList) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(integer);
            GoodsListDTO goodsListDTO = new GoodsListDTO();
            goodsListDTO.setId(marketGoods.getId());
            goodsListDTO.setName(marketGoods.getName());
            goodsListDTO.setBrief(marketGoods.getBrief());
            goodsListDTO.setPicUrl(marketGoods.getPicUrl());
            goodsListDTO.setIsNew(marketGoods.getIsNew());
            goodsListDTO.setIsHot(marketGoods.getIsHot());
            goodsListDTO.setCounterPrice(marketGoods.getCounterPrice());
            goodsListDTO.setRetailPrice(marketGoods.getRetailPrice());
            goodsListDTOList.add(goodsListDTO);
        }
        DataDTOXX dataDTOXX = new DataDTOXX();
        dataDTOXX.setTopic(topicDTO);
        dataDTOXX.setGoodsList(goodsListDTOList);
        return dataDTOXX;
    }

    @Override
    public BaseRespVo update(TopicUpdateVO topic) {
        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setId(topic.getId());
        marketTopic.setTitle(topic.getTitle());
        marketTopic.setSubtitle(topic.getSubtitle());
        marketTopic.setPrice(topic.getPrice());
        marketTopic.setReadCount(topic.getReadCount());
        marketTopic.setPicUrl(topic.getPicUrl());
        marketTopic.setSortOrder(topic.getSortOrder());
        if (topic.getGoods()!=null){
            String value = "";
            try {
                value = objectMapper.writeValueAsString(topic.getGoods());
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            marketTopic.setGoods(value);
        }

        marketTopic.setAddTime(topic.getAddTime());
        marketTopic.setUpdateTime(new Date());
        marketTopic.setDeleted(topic.getDeleted());
        marketTopic.setContent(topic.getContent());
        try {
            int i = marketTopicMapper.updateByPrimaryKeySelective(marketTopic);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidData();
        }
        return BaseRespVo.ok(topic);
    }

    @Override
    public Boolean create(TopicCreateBO topicCreateBO) {
        MarketTopic topic = new MarketTopic();
        topic.setTitle(topicCreateBO.getTitle());
        topic.setSubtitle(topicCreateBO.getSubtitle());
        topic.setPrice(topicCreateBO.getPrice());
        topic.setReadCount(topicCreateBO.getReadCount());
        topic.setPicUrl(topicCreateBO.getPicUrl());
        if (topicCreateBO.getGoods()!=null){
            String value = "";
            try {
                value = objectMapper.writeValueAsString(topicCreateBO.getGoods());
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            topic.setGoods(value);
        }

        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());
        topic.setDeleted(false);
        topic.setContent(topicCreateBO.getContent());
        try {
            marketTopicMapper.insertSelective(topic);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }

    @Override
    public boolean delete(Integer id) {
        try {
            MarketTopic topic = marketTopicMapper.selectByPrimaryKey(id);
            topic.setDeleted(true);
            marketTopicMapper.updateByPrimaryKeySelective(topic);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean batchDelete(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                MarketTopic topic = marketTopicMapper.selectByPrimaryKey(id);
                topic.setDeleted(true);
                marketTopicMapper.updateByPrimaryKeySelective(topic);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
